package cn.cloudplug.friendsrecommend.util;

import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMDataListener;
import com.umeng.socialize.exception.SocializeException;

public class LoginOrRegisterUtil {
	private Activity activity;
	private UMSocialService mController;
	public LoginOrRegisterUtil(Activity activity){
		mController = UMServiceFactory
				.getUMSocialService("com.umeng.share");
		this.activity=activity;
	}
	/**
     * 授权。如果授权成功，则获取用户信息</br>
     */
    public void login(final SHARE_MEDIA platform) {
        mController.doOauthVerify(activity, platform, new UMAuthListener() {

            @Override
            public void onStart(SHARE_MEDIA platform) {
            }

            @Override
            public void onError(SocializeException e, SHARE_MEDIA platform) {
            }

            @Override
            public void onComplete(Bundle value, SHARE_MEDIA platform) {
                String uid = value.getString("uid");
                if (!TextUtils.isEmpty(uid)) {
                    getUserInfo(platform);
                } else {
                    Toast.makeText(activity, "授权失败...", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancel(SHARE_MEDIA platform) {
            }
        });
    }

    /**
     * 获取授权平台的用户信息</br>
     */
    public void getUserInfo(SHARE_MEDIA platform) {
        mController.getPlatformInfo(activity, platform, new UMDataListener() {

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(int status, Map<String, Object> info) {
//                String showText = "";
//                if (status == StatusCode.ST_CODE_SUCCESSED) {
//                    showText = "用户名：" + info.get("screen_name").toString();
//                    Log.d("#########", "##########" + info.toString());
//                } else {
//                    showText = "获取用户信息失败";
//                }
                if ( info != null ) {
                    Toast.makeText(activity, info.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
